
package com.viweb.storemanager.core;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class DateUtil {

    /** Creates a new instance of DateUtil */
    public DateUtil() {
    }

    public static String getShortDatePattern(Locale locale) {
        if (locale.equals(Locale.ENGLISH)) {
            return "MM/dd/yyyy";
        } else if (locale.equals(Locale.FRENCH)) {
            return "dd/MM/yyyy";
        } else {
            return "MM/dd/yyyy";
        }
    }

    public static String getShortDateTimePattern(Locale locale) {
        if (locale.equals(Locale.ENGLISH)) {
            return "MM/dd/yyyy HH:mm";
        } else if (locale.equals(Locale.FRENCH)) {
            return "dd/MM/yyyy HH:mm";
        } else {
            return "MM/dd/yyyy HH:mm";
        }
    }

    public static String getDefaultDatePattern(Date date) {
        DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT,
                Locale.getDefault());

        if (date == null) {
            return "";
        }

        return dateFormatter.format(date);
    }

    public static String getDefaultDateTimePattern(Date date) {
        DateFormat dateTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                DateFormat.SHORT,
                Locale.getDefault());

        if (date == null) {
            return "";
        }

        return dateTimeFormatter.format(date);
    }

    public static String getENDatePattern(Date date) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        return formatter.format(date);
    }

    public static String getFRDatePattern(Date date) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        return formatter.format(date);
    }

    public static String getDatePattern(Date date, Locale locale) {
        if (date == null) {
            return "";
        }

        if (locale.equals(Locale.ENGLISH)) {
            return getENDatePattern(date);
        } else if (locale.equals(Locale.FRENCH)) {
            return getFRDatePattern(date);
        } else {
            return getDefaultDatePattern(date);
        }
    }

    public static String getENDateTimePattern(Date date) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm");
        return formatter.format(date);
    }

    public static String getFRDateTimePattern(Date date) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        return formatter.format(date);
    }

    public static String getDateTimePattern(Date date, Locale locale) {
        if (date == null) {
            return "";
        }

        if (locale.equals(Locale.ENGLISH)) {
            return getENDateTimePattern(date);
        } else if (locale.equals(Locale.FRENCH)) {
            return getFRDateTimePattern(date);
        } else {
            return getDefaultDateTimePattern(date);
        }
    }

    public static String getVNTimePattern(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        return formatter.format(date);
    }

    public static String getVNDatePattern(Date date) {
        return getFRDatePattern(date);
    }

    public static String getVNTimestampPattern(Date date) {
        return getFRDateTimePattern(date);
    }

    public static String getSQLDatePattern(Date date) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    public static String getSQLTimestampPattern(Date date) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    public static java.sql.Date convert2SQLDate(Date date) {
        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        java.sql.Date sqlDate = new java.sql.Date(calendar.getTimeInMillis());

        return sqlDate;
    }

    public static Timestamp convert2SQLTimestamp(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Timestamp sqlTimestamp = new Timestamp(calendar.getTimeInMillis());

        return sqlTimestamp;
    }

    public static Date convertVNDatePattern2Date(String input) {
        Date date = new Date();

        try {
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            date = format.parse(input);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return date;
    }

    public static String getSecond(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        return formatter.format(date).substring(17);
    }

    public static String getMinute(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        return formatter.format(date).substring(14, 16);
    }

    public static String getHour(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        return formatter.format(date).substring(11, 13);
    }

    public static String getDay(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        return formatter.format(date).substring(0, 2);
    }

    public static String getMonth(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        return formatter.format(date).substring(3, 5);
    }

    public static Date getLastDateByMonthSpace(int monthSpace) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -monthSpace);
        return c.getTime();
    }

    public static Date getLastDateByDaySpace(int daySpace) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -daySpace);
        return c.getTime();
    }

    public static Date getFirstDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    public static Date getLastDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    public static String getYear(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        return formatter.format(date).substring(6);
    }

    public static Integer getHours(int duration) {
        return new Integer(duration / 60);
    }

    public static Integer getMinutes(int duration) {
        return new Integer(duration % 60);
    }

    public static Date addYear(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, amount);

        return calendar.getTime();
    }

    public static Date addMonth(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, amount);

        return calendar.getTime();
    }

    public static Date addDate(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, amount);

        return calendar.getTime();
    }

    public static Date subYear(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 0 - amount);

        return calendar.getTime();
    }

    public static Date subMonth(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0 - amount);

        return calendar.getTime();
    }

    public static Date subDate(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 0 - amount);

        return calendar.getTime();
    }

    public static int compareTo(Date date, Date anotherDate) {
        if (date == null) {
            return -1;
        }

        if (anotherDate == null) {
            return 1;
        }

        Calendar anotherCal = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();

        cal.setTime(date);
        anotherCal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
        anotherCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        anotherCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        date = anotherCal.getTime();

        cal.setTime(anotherDate);
        anotherCal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
        anotherCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        anotherCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        anotherDate = anotherCal.getTime();

        return date.compareTo(anotherDate);
    }

    public static String createUniqueFilename() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH'h'mm'm'ss's'");
        return formatter.format(new Date());
    }

    public static Date getDateTime(Date date, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);

        return calendar.getTime();
    }
}
